package com.kzx.blog.service.impl;

import com.kzx.blog.config.model.FailedException;
import com.kzx.blog.config.model.FailedInfo;
import com.kzx.blog.mapper.BlogMapper;
import com.kzx.blog.model.Blog;
import com.kzx.blog.model.Token;
import com.kzx.blog.service.IBlog;
import com.kzx.blog.service.model.BlogAddParam;
import com.kzx.blog.service.model.BlogPagingParam;
import com.kzx.blog.service.model.BlogUpdateParam;
import com.kzx.blog.util.DateUtils;
import com.kzx.blog.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author kongz
 * @version 1.0 2021/11/29 10:40
 * @description blog service接口实现类
 */
@Service
public class BlogImpl implements IBlog {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public PageUtils<Blog> getBlogs(BlogPagingParam param) {
        Map<String, Object> map = PageUtils.getPagingMap(param);
        if (param.getAuthor() > 0) {
            map.put("author", param.getAuthor());
        }
        if (param.getStatus() >= 0) {
            map.put("status", param.getStatus());
        }
        if (param.getType() >= 0) {
            map.put("type", param.getType());
        }
        if (param.getTop() >= 0) {
            map.put("top", param.getTop());
        }
        if (param.getTitle() != null && !param.getTitle().replace(" ", "").isEmpty()) {
            map.put("title", "%" + param.getTitle() + "%");
        }

        return new PageUtils<>(param.getPageIndex(), param.getPageSize(), blogMapper.getCount(map), blogMapper.get(map));
    }

    @Override
    public Blog getBlog(int id) {
        return blogMapper.getById(id);
    }

    @Override
    public int update(BlogUpdateParam blogUpdateParam, Token token) {

        Blog blog = blogMapper.getById(blogUpdateParam.getId());
        if (blog.getAuthor() != token.getUid()) { // 只有自己可以更改自己的博客
            throw new FailedException(FailedInfo.NO_PERMISSION.getErrorCode(), FailedInfo.NO_PERMISSION.getErrorMsg());
        }

        blog.setContent(blogUpdateParam.getContent());
        blog.setUpdatedTime(DateUtils.getTime());
        blog.setStatus(blogUpdateParam.getStatus());

        try {
            return blogMapper.update(blog);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FailedException(FailedInfo.ERROR_UPDATE.getErrorCode(), FailedInfo.ERROR_UPDATE.getErrorMsg());
        }
    }

    @Override
    public int delete(int blogId, Token token) {

        Blog blog = blogMapper.getById(blogId);
        if (blog.getAuthor() != token.getUid()) { // 只有自己可以更改自己的博客
            throw new FailedException(FailedInfo.NO_PERMISSION.getErrorCode(), FailedInfo.NO_PERMISSION.getErrorMsg());
        }

        blog.setStatus(Blog.STATUS.DELETED);

        try {
            return blogMapper.update(blog);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FailedException(FailedInfo.ERROR_UPDATE.getErrorCode(), FailedInfo.ERROR_UPDATE.getErrorMsg());
        }
    }

    @Override
    public int top(int blogId, Token token) {
        Blog blog = blogMapper.getById(blogId);
        if (blog.getAuthor() != token.getUid()) { // 只有自己可以更改自己的博客
            throw new FailedException(FailedInfo.NO_PERMISSION.getErrorCode(), FailedInfo.NO_PERMISSION.getErrorMsg());
        }

        if (blog.getTop() == Blog.TOP.IS_TOP) {
            blog.setTop(Blog.TOP.NOT_TOP);
        } else {
            blog.setTop(Blog.TOP.IS_TOP);
        }


        try {
            return blogMapper.update(blog);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FailedException(FailedInfo.ERROR_UPDATE.getErrorCode(), FailedInfo.ERROR_UPDATE.getErrorMsg());
        }
    }

    @Override
    public int add(BlogAddParam blogAddParam, Token token) {

        Blog blog = new Blog();
        blog.setAuthor(token.getUid());
        blog.setTitle(blogAddParam.getTitle());
        blog.setContent(blogAddParam.getContent());
        blog.setCreatedTime(DateUtils.getTime());
        blog.setUpdatedTime(DateUtils.getTime());
        blog.setStatus(Blog.STATUS.NOT_RELEASED);
        blog.setType(blogAddParam.getType());
//        if (blogAddParam.getType() == Blog.TYPE.ORIGINAL) {// 原创
            blog.setSrcUrl(null);
//        }
        blog.setTop(Blog.TOP.NOT_TOP);

        try {
            blogMapper.add(blog);
            return blog.getId();
        } catch (Exception e) {
            e.printStackTrace();
            throw new FailedException(FailedInfo.ERROR_INSERT.getErrorCode(), FailedInfo.ERROR_INSERT.getErrorMsg());
        }
    }
}
